package source.view;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;

import source.model.ObjectID;
import source.model.Unit;
import source.controller.PlayerKeyListener;

/**
 * Displays all units and their statistics of the current player inside of a table.  
 * @author Steve
 *
 */
public class UnitOverview extends JPanel{
	
	private ViewData viewData;
	
	/**
	 * The Model of our table.  This is used to maintain what is displayed by our Units table.
	 */
	class UneditableCellsModel extends AbstractTableModel{
		private String[] columnNames;
		private Object[][] rowData;

		public UneditableCellsModel(String[] columnsNames, Object[][] data){
			this.columnNames = columnsNames;
			this.rowData = data;
		}
		
		public void setColumnNames(String[] colNames){
			this.columnNames = colNames;
		}

		public boolean isCellEditable(int row, int col){
			return false;
		}
		public String getColumnName(int col){
			return columnNames[col].toString();
		}
		public int getRowCount(){
			return rowData.length;
		}
		public int getColumnCount(){
			return columnNames.length;
		}
		public Object getValueAt(int row, int col){
			return rowData[row][col];
		}
		public void setValueAt(Object value, int row, int col){
			rowData[row][col] = value;
			fireTableCellUpdated(row, col);
		}
		public void setRowData(Object[][] rowData){
			this.rowData = rowData;
		}
	}
	private JTable table;
	private UneditableCellsModel tableModel;
	private String[] colNames = {"Unit Type", "Current Health", "Maximum Health","Movement Rate","View Distance", "Moves Remaining"};
	
	private class MyListSelectionListener implements ListSelectionListener{

		public void valueChanged(ListSelectionEvent e) {
			if(e.getValueIsAdjusting()) return ;
			Vector<Unit> units = viewData.getUnitsForPlayer();
			viewData.clearSelectedObjectID();
			for(int i = 0; i < units.size(); i++){
				if(table.isRowSelected(i)){
					viewData.addSelectedObjectID(units.get(i).objectID());
				}
			}
		}
	}

	public UnitOverview(ViewData viewData, PlayerKeyListener pkl){
		this.viewData = viewData;
		table = new JTable();
		table.addKeyListener(pkl);
		this.setLayout(new GridLayout(1,0));
	}
	
	public void removeKeyListener(PlayerKeyListener pkl){
		//table.removeKeyListener(pkl);
	}

	public void removeChangeListener(PlayerKeyListener pkl){
		//table.addKeyListener(pkl);	
	}

	public void update(){
		this.removeAll();
		
		Vector<Unit> units = viewData.getUnitsForPlayer();
		Object[][] data = new Object[units.size()][colNames.length];
		
		for(int i = 0; i < units.size(); i++){
			Hashtable<String, String> stats = units.get(i).getViewData();
			for(int j = 0; j < colNames.length; j++){
				data[i][j] = stats.get(colNames[j]);
			}
		}
		UneditableCellsModel tableModel = new UneditableCellsModel(colNames, data);
		table.setModel(tableModel);
		table.getSelectionModel().addListSelectionListener(new MyListSelectionListener());
		this.add(new JScrollPane(table));
	}
	
	public void setColumnNames(String[] cols){
		tableModel.setColumnNames(cols);
	}
	
	public void setData(Object[][] data){
		tableModel.setRowData(data);
	}
}
